#_*_ coding: UTF-8 _*_

from core.db import *
from core.qpath import *
from core import log

def temp():
	pass
	
makeAllMethodsStatic =	"""
for method in dir():
	if type(eval(method)) is type(temp):
		exec(method+' = staticmethod('+method+')')"""

#class Aye(SQLObject):
	
	#def matn(self):
		#if not hasattr(self, 'matnCache') or self.matnCache==None:
			#self.matnCache = u''
			#for nk in self.nemooneKalamat:
				#if (nk.analyse==1):
					#self.matnCache = self.matnCache + finglishToUnicode(nk.kalame) + u' '
		#return self.matnCache
	
	#def matnWithHighlight(self, highlights):
		#if not hasattr(self, 'matnCacheWH') or self.matnCacheWH==None:
			#self.matnCacheWH = u''
			#curH = 0
			#curW = 0
			#for nk in self.nemooneKalamat:
				#if (nk.analyse==1):
					#if curW != highlights[curH]:
						#self.matnCacheWH = self.matnCacheWH + finglishToUnicode(nk.kalame) + u' '
					#else:
						#self.matnCache = self.matnCacheWH + u'<b>' + finglishToUnicode(nk.kalame) + u'</b> '
						#curH = curH + 1
					#curW = curW + 1
						
		#return self.matnCacheWH

class Quran(object):
	def getAllSooreh():
		return get('*/')
		
	def getSoorehByName(name): #these are static functions
		return get('%s/' % name)
	
	def getSoorehByNumber(number):
		return get('%d/' % number)
	
	#def getSafhe(number):
		#return Safhe.byNumber(number)
	
	#def getAye(numberInQuran):
		#return Aye.byNumber(numberInQuran)
	def findAdvancedAyat(expression):
		result = get(expression.encode("utf-8"))
		x = []
		if type(result)!=type([]):
			result = [result]
		for a in result:
			if isinstance(a,qSure):
				x = x + [[b,[]] for b in a.ayat]
			if isinstance(a,qAye):
				x.append([a,[]])
			if isinstance(a,qWord):
				x.append([a.aye,[a.n]])
		return x		


	def findAyat(expression, firstSooreh, firstAye, lastSooreh, lastAye, withErab, wholeWord):
		""" Return list<Aye, list<Int>> where 'Int' are highlighted words in aye. """
		if wholeWord:
			expression = ' '+expression.strip()+' '
		if withErab:
			kalamat = get('w:%d-%d/*%s*' % (firstSooreh.id, lastSooreh.id, expression.encode('utf8')))
		else:
			kalamat = get('w:%d-%d/N*%s*' % (firstSooreh.id, lastSooreh.id, expression.encode('utf8')))
		
		ayeWithHL = list()
		try:
			count = 0
			ayeWithHL.append([kalamat[0].aye, [kalamat[0].n]])
			for k in kalamat[1:]:
				if type(k) is qWord:
					if k.aye==ayeWithHL[count][0]:
						ayeWithHL[count][1].append(k.n)
					else:
						ayeWithHL.append([k.aye, [k.n]])
						count = count + 1
		except:
			pass
			
		return ayeWithHL
	
	exec(makeAllMethodsStatic)

#------------------------------------------------------------------------------------

if __name__ == '__main__':
	def testMatnWithHL():
		aye = Quran.getSoorehByNumber(1).ayat[0]
		print aye.noErabData.data
		
	testMatnWithHL()
